import { createRouter, createWebHashHistory } from "vue-router";
import type { RouteRecordRaw } from "vue-router";
import {
  HomeFilled,
  Opportunity,
  List,
  Histogram,
} from "@element-plus/icons-vue";
import store from "@/store";
import { h, resolveComponent } from "vue";

type IconType = typeof HomeFilled;

const RouteView = {
  render() {
    return h(resolveComponent("RouterView"));
  },
};

export type MyRoute = RouteRecordRaw & {
  children?:MyRoute[];
  meta?: {
    title: string;
    icon?: IconType;
    hidden?: Boolean;
    [key: string]: any;
  };
};
export const routes: MyRoute[] = [
  {
    path: "/",
    name: "index",
    redirect: "/home",
    component: () => import("@/components/BasicLayout.vue"),
    children: [
      {
        path: "/home",
        name: "home",
        component: () => import("@/views/Home.vue"),
        meta: {
          title: "主页",
          icon: HomeFilled,
        }
      },
      {
        path: "/resource",
        name: "resource",

        redirect: "/resource/kol",
        component: RouteView,
        meta: {
          title: "资源",
          icon: Opportunity,
        },
        children: [
          {
            path: "/resource/kol",
            name: "kol",
            component: () => import("@/views/resource/kol/index.vue"),
            meta: {
              title: "博主",
            },
          },
          {
            path: "/resource/organization",
            name: "organization",
            component: () => import("@/views/resource/organization/index.vue"),
            meta: {
              title: "机构",
            },
          },
        ],
      },

      {
        path: "/project",
        name: "project",

        redirect: "/project/requirement",
        component: RouteView,
        meta: {
          title: "项目",
          icon: List,
        },
        children: [
          {
            path: "/project/requirement",
            name: "projectRequirement",
            component: () => import("@/views/project/requirement/index.vue"),
            meta: {
              title: "需求",
            },
          },
          {
            path: "/project/execution",
            name: "projectExecution",
            component: () => import("@/views/project/execution/index.vue"),
            meta: {
              title: "执行",
            },
          },
          {
            path: "/project/execution/detail/:id",
            name: "executionDetail",
            component: () => import("@/views/project/execution/detail/index.vue"),
            meta: {
              title: "详情",
              hidden: true,
            },
          },
        ],
      },
      {
        path: "/overview",
        name: "overview",
        component: () => import("@/views/Overview.vue"),
        meta: {
          title: "总览",
          icon: Histogram,
        },
      },
    ],
  },

  {
    path: "/login",
    name: "login",

    component: () => import("@/views/user/login/index.vue"),
    meta: {
      title: "登录",
    },
  },
  { path: '/:pathMatch(.*)*', name: 'NotFound', component: () => import("@/views/404.vue") },
];
const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
router.beforeEach((to, from, next) => {
  if (to.name !== "login") {
    if (!store.state.isLogin) {
      const username = sessionStorage.getItem("isLogin");
      if (username) {
        store.commit("login", username);
        next();
        return;
      }
      next({ path: "/login" });
      return;
    }
  }
  next();
});
export default router;
